package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserAddress;
import com.tr.rentcar.model.bean.UserJf;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;
/**
 *积分dao 
 * @author lenovo
 *
 */
public class UserJfDao implements ResultHandler<UserJf>{
	private MySqlDb db= MySqlDb.getInstance();
	/**
	 *查寻所有 
	 */
	public List<UserJf> findAll() throws SQLException {
		String sql = "select * from  user_jf";
		return db.executeQuery(sql,this);
	}
	/**
	 * 通过id查寻
	 */
	public List<UserJf> findByid(int uid,int c) throws SQLException {
		StringBuffer sql=new StringBuffer("select * from user_jf where uid = "+uid+" order by uj_time desc");
		sql.append(" limit "+c+",13");
		return db.executeQuery(sql.toString(), this);
	}
	/**
	 * 通过用户ID查询记录数
	 */
	public long getAllCount(int uid) throws SQLException{
		String sql="select count(*)as count from user_jf where uid=?";
		return (long) db.executeCountQuery(sql,new GlobalParamHandler(uid));
	}
	/**
	 * 评价订单获得积分
	 */
	public boolean pingjia(int ujScore,String ujReason,int uid) throws SQLException{
		String sql="insert into `user_jf`(uj_score,uj_reason,uid) values(?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(ujScore,ujReason,uid))>0;
		
	}
	/**
	 * 插入一条积分的消费记录
	 * @param ujTime
	 * @param ujScore
	 * @param ujReason
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public boolean insertRecord(Date ujTime,int ujScore,String ujReason,int uid) throws SQLException{
		String sql="insert into `user_jf`(uj_time,uj_score,uj_reason,uid) values(?,?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(ujTime,ujScore,ujReason,uid))>0;
		
	}
	
	@Override
	public UserJf doHander(Map<String, Object> row) {
		UserJf uj=new UserJf();
		uj.setUjId(Integer.parseInt(row.get("uj_id")+""));
		uj.setUjTime((Date)row.get("uj_time"));
		uj.setUjScore(Integer.parseInt(row.get("uj_score")+"") );
		uj.setUjReason((String)row.get("uj_reason"));
		return uj;
	}

}
